#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int MAXN=1e5+10;
LL f[MAXN],c[5],d[5];
int main()
{
	for(LL i=1;i<=4;++i)scanf("%lld",&c[i]);
	f[0]=1;
	for(LL i=1;i<=4;++i)
		for(LL j=c[i];j<=100000;++j)
			f[j]+=f[j-c[i]];
	LL T;
	scanf("%lld",&T);
	while(T--)
	{
		for(LL i=1;i<=4;++i)
			scanf("%lld",&d[i]),++d[i];
		LL s,ans;
		scanf("%lld",&s);
		ans=f[s];
		for(LL i=1;i<=4;++i)if(s>=d[i]*c[i])ans-=f[s-d[i]*c[i]];
		for(LL i=1;i<=3;i++)for(LL j=i+1;j<=4;j++)if(s>=d[i]*c[i]+d[j]*c[j])ans+=f[s-d[i]*c[i]-d[j]*c[j]];
		for(LL i=1;i<=2;i++)for(LL j=i+1;j<=3;j++)for(LL k=j+1;k<=4;k++)if(s>=d[i]*c[i]+d[j]*c[j]+d[k]*c[k])ans-=f[s-d[i]*c[i]-d[j]*c[j]-d[k]*c[k]];
		if(s>=d[1]*c[1]+d[2]*c[2]+d[3]*c[3]+d[4]*c[4])ans+=f[s-d[1]*c[1]-d[2]*c[2]-d[3]*c[3]-d[4]*c[4]];
		printf("%lld\n",ans);
	}
	return 0;
}
